package com.atguigu.tms.mock.service.impl;

import com.atguigu.mock.util.MyDateUtil;
import com.atguigu.mock.util.RandomBox;
import com.atguigu.mock.util.RandomNum;
import com.atguigu.mock.util.RandomNumString;
import com.atguigu.tms.mock.bean.BaseOrgan;
import com.atguigu.tms.mock.bean.EmployeeInfo;
import com.atguigu.tms.mock.bean.TruckDriver;
import com.atguigu.tms.mock.bean.TruckTeam;
import com.atguigu.tms.mock.config.AppConfig;
import com.atguigu.tms.mock.constant.TmsConstant;
import com.atguigu.tms.mock.mapper.TruckDriverMapper;
import com.atguigu.tms.mock.service.EmployeeInfoService;
import com.atguigu.tms.mock.service.TruckDriverService;
import com.atguigu.tms.mock.service.TruckTeamService;
import com.atguigu.tms.mock.service.adv.impl.AdvServiceImpl;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 司机驾驶证表 服务实现类
 * </p>
 *
 * @author zhangchen
 * @since 2022-06-18
 */
@Service
public class TruckDriverServiceImpl extends AdvServiceImpl<TruckDriverMapper, TruckDriver> implements TruckDriverService {

    @Autowired
    TruckTeamService truckTeamService;

    @Autowired
    EmployeeInfoService employeeInfoService;

    public TruckDriver  initTruckDriver(Long orgId){

        EmployeeInfo  employeeInfo  = employeeInfoService.initEmployeeInfo(orgId,  TmsConstant.POSITION_DRIVER);
        TruckTeam truckTeam = truckTeamService.getById(orgId, true);

        TruckDriver truckDriver = new TruckDriver();
        truckDriver.setEmpId(employeeInfo.getId());
        truckDriver.setLicensePictureUrl("http://"+ RandomStringUtils.random(10,true,true));
        truckDriver.setLicenseNo(employeeInfo.getIdCard());
        RandomBox ifLicenseTypeABox = new RandomBox(7, 3);
        if (ifLicenseTypeABox.getRandBoolValue()){
            truckDriver.setLicenseType("A") ;
        }else{
            truckDriver.setLicenseType("B");
        }

        int betweenGradToEmpYear = MyDateUtil.getBetweenYear(employeeInfo.getGraduationDate(), employeeInfo.getEmploymentDate());
        int driverAge = RandomNum.getRandInt(0, betweenGradToEmpYear);
        Date initLicenseDate = DateUtils.addYears(employeeInfo.getGraduationDate(), driverAge);
        truckDriver.setInitLicenseDate(initLicenseDate);

        Date expireDate = DateUtils.addYears(AppConfig.mock_date, RandomNum.getRandInt(3,10));
        truckDriver.setExpireDate(expireDate);

        truckDriver.setIsDeleted("0");
        truckDriver.setIsEnabled("1");
        truckDriver.setTeamId(truckTeam.getId());
        truckDriver.setOrgId(orgId);
        truckDriver.setCreateTime(AppConfig.mock_date);

        saveOrUpdate(truckDriver,true);

         return  truckDriver;
    }
}
